﻿using System;
using System.ComponentModel;
using System.Windows;
using nRoute.Components;
#if (WINRT)
using Windows.UI.Xaml.Markup;
using Windows.UI.Xaml;
using System.Windows.Interactivity;
using Windows.UI.Xaml.Data;
#else
using System.Windows.Markup;
#endif

namespace nRoute.Behaviors
{
#if (WINRT)
    [ContentProperty(Name = "Parameters")]
#else
    [ContentProperty("Parameters")]
#endif
    public abstract class NavigateActionBase
        : NavigationHandlerActionBase
    {

#region Constants and Variables

        public static readonly DependencyProperty UrlProperty = 
            DependencyProperty.Register("Url", typeof(string), typeof(NavigateActionBase), 
            new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));

        public static readonly DependencyProperty ParametersProperty =
            DependencyProperty.Register("Parameters",
//#if (WP7)
//            typeof(ParametersCollection), typeof(NavigateActionBase), null);
//#else
            typeof(DependencyParameterCollection), typeof(NavigateActionBase), null);
//#endif
            
        public static readonly DependencyProperty SiteAreaProperty =
            DependencyProperty.Register("SiteArea", typeof(string), typeof(NavigateActionBase),
            new PropertyMetadata(Utilities.Compatability.DP_UNSET_VALUE));

#endregion

#region Properties

        [Category("Common Properties")]
//#if WP7
//        [TypeConverter(typeof(nRoute.Components.TypeConverters.ConvertFromStringConverter))]
//#endif
        public string Url
        {
            get { return Convert.ToString(GetValue(UrlProperty)); }
            set { SetValue(UrlProperty, value); }
        }

//#if (WP7)

//        [Category("Common Properties")]
//        public ParametersCollection Parameters
//        {
//            get 
//            {
//                // lazy load, not the best idea but with DP's - but this way we can avoid unnecessary creation of parameters
//                if (GetValue(ParametersProperty) == null)
//                {
//                    SetValue(ParametersProperty, new ParametersCollection());
//                }
//                return (ParametersCollection)GetValue(ParametersProperty); 
//            }
//            set { SetValue(ParametersProperty, value); }
//        }

//#else

        [Category("Common Properties")]
        public DependencyParameterCollection Parameters
        {
            get 
            {
                // lazy load, not the best idea but with DP's - but this way we can avoid unnecessary creation of parameters
                if (GetValue(ParametersProperty) == null)
                {
                    SetValue(ParametersProperty, new DependencyParameterCollection());
                }
                return (DependencyParameterCollection)GetValue(ParametersProperty);
            }
            set { SetValue(ParametersProperty, value); }
        }        

//#endif

        [Category("Common Properties")]
        public string SiteArea
        {
            get { return Convert.ToString(GetValue(SiteAreaProperty)); }
            set { SetValue(SiteAreaProperty, value); }
        }

#if (WINRT)

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding UrlBinding
        {
            get { return GetBinding(UrlProperty); }
            set { SetBinding<string>(UrlProperty, value); }
        }

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding ParametersBinding
        {
            get { return GetBinding(ParametersProperty); }
            set { SetBinding<DependencyParameterCollection>(ParametersProperty, value); }
        }

        [Category("Common Properties")]
        [CustomPropertyValueEditor(CustomPropertyValueEditor.PropertyBinding)]
        public Binding SiteAreaBinding
        {
            get { return GetBinding(SiteAreaProperty); }
            set { SetBinding<string>(SiteAreaProperty, value); }
        }

#endif

#endregion

#region Overrides

//#if WP7
        
//        protected override void OnAttached()
//        {
//            // note we need to manually attach
//            var _parameters = GetValue(ParametersProperty) as IAttachedObject;
//            if (_parameters != null) _parameters.Attach(this.AssociatedObject);
//            base.OnAttached();
//        }

//        protected override void OnDetaching()
//        {
//            // note we need to manually detach
//            var _parameters = GetValue(ParametersProperty) as IAttachedObject;
//            if (_parameters != null) _parameters.Detach();
//            base.OnDetaching();
//        }

//#endif

#endregion

    }
}
